package com.jiangjiang.eshop.customer.service.impl;

import com.jiangjiang.eshop.customer.constant.ReturnGoodsWorksheetStatus;
import com.jiangjiang.eshop.customer.domain.ReturnGoodsWorksheetEntity;
import com.jiangjiang.eshop.customer.service.CustomerFacadeService;
import com.jiangjiang.eshop.customer.service.ReturnGoodsWorksheetService;
import com.jiangjiang.eshop.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jianghaowen
 * @Date 2021/9/9
 * @Description: TODO
 * @Version V1.0
 **/
@Slf4j
@Component
public class CustomerFacadeServiceImpl implements CustomerFacadeService {
    @Autowired
    private ReturnGoodsWorksheetService returnGoodsWorksheetService;
    @Autowired
    private OrderService orderService;

    /**
     * 创建退货工单
     *
     * @param orderId            订单 id
     * @param orderNo            订单 编号
     * @param returnGoodsReason  退货原因
     * @param returnGoodsComment 退货备注
     * @return 处理结果
     */
    @Override
    public Boolean createReturnGoodsWorksheet(Long orderId, String orderNo, Integer returnGoodsReason, String returnGoodsComment) {
        try {
            ReturnGoodsWorksheetEntity entity = new ReturnGoodsWorksheetEntity();
            entity.setOrderInfoId(orderId);
            entity.setOrderNo(orderNo);
            entity.setReturnGoodsReason(returnGoodsReason);
            entity.setReturnGoodsRemark(returnGoodsComment);
            entity.setReturnGoodsReason(ReturnGoodsWorksheetStatus.WAIT_FOR_APPROVE);
            return returnGoodsWorksheetService.save(entity);
        } catch (Exception e){
            log.warn("CustomerFacadeServiceImpl createReturnGoodsWorksheet error",e);
        }
        return false;

    }

    /***
     * 同步物流单号
     * @param orderInfoId 订单id
     * @param returnGoodsLogisticsCode 退货物流单号
     * @return 处理结果
     */
    @Override
    public Boolean syncReturnGoodsLogisticsCode(Long orderInfoId, String returnGoodsLogisticsCode) {
        try {
            ReturnGoodsWorksheetEntity entity = returnGoodsWorksheetService.queryByOrderInfoId(orderInfoId);
            entity.setReturnGoodsLogisticsCode(returnGoodsLogisticsCode);
            entity.setStatus(ReturnGoodsWorksheetStatus.WAIT_FOR_RECEIVE_RETURN_GOODS);
            returnGoodsWorksheetService.updateById(entity);
        }catch (Exception e){
            log.warn("CustomerFacadeServiceImpl syncReturnGoodsLogisticsCode error",e);
        }
        return false;
    }


    /**
     * 通知客服中心，完成退货入库事件发生了
     * @param returnGoodsWorkSheetId 退货工单id
     * @return 处理结果
     */
    @Override
    public Boolean informReturnGoodsInputFinishedEvent(Long returnGoodsWorkSheetId) {
       try {
           ReturnGoodsWorksheetEntity entity = returnGoodsWorksheetService.getById(returnGoodsWorkSheetId);
           entity.setStatus(ReturnGoodsWorksheetStatus.FINISH_RETURN_GOODS_INPUT);
           returnGoodsWorksheetService.updateById(entity);

           orderService.informReturnGoodsInputOrderApprovedEvent(entity.getOrderInfoId());
       }catch (Exception e){
           log.warn("CustomerFacadeServiceImpl informReturnGoodsInputFinishedEvent error",e);
       }
        return false;
    }


    /**
     * 通知客服中心 “完成退款”事件发生了
     * @param returnGoodsWorkSheetId 退货工单id
     * @return 处理结果
     */
    @Override
    public Boolean informRefundFinishedEvent(Long returnGoodsWorkSheetId) {
        try {
            ReturnGoodsWorksheetEntity entity= returnGoodsWorksheetService.getById(returnGoodsWorkSheetId);
            entity.setStatus(ReturnGoodsWorksheetStatus.FINISH_RETURN_GOODS_REFUND);
            returnGoodsWorksheetService.updateById(entity);

            orderService.informRefundFinishedEvent(entity.getOrderInfoId());
            return true;
        }catch (Exception e){
            log.warn("CustomerFacadeServiceImpl informRefundFinishedEvent error",e);
        }
        return false;
    }
}
